package cn.getech.data.metadata.service.impl;

import cn.getech.data.metadata.entity.CollectionNotificationReceived;
import cn.getech.data.metadata.mapper.CollectionNotificationReceivedMapper;
import cn.getech.data.metadata.service.CollectionNotificationReceivedService;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service("collectionNotificationReceivedService")
public class CollectionNotificationReceivedServiceImpl extends ServiceImpl<CollectionNotificationReceivedMapper, CollectionNotificationReceived> implements CollectionNotificationReceivedService {

    @Autowired
    private  CollectionNotificationReceivedMapper  collectionNotificationReceivedMapper;

    /**
     * 依照用户或者邮箱查询 通知人 模糊查询
     * @param userEmail
     * @return
     */
    @Override
    public List<CollectionNotificationReceived> queryByUserOremail(String userEmail) {
        LambdaQueryWrapper<CollectionNotificationReceived> queryWrapper = Wrappers.lambdaQuery();
        if(StringUtils.isNotBlank(userEmail)){
            queryWrapper.and(wrapper -> wrapper.and(wrapper1 -> wrapper1.like(CollectionNotificationReceived::getEmail, userEmail))
                        .or(wrapper2 -> wrapper2.like(CollectionNotificationReceived::getReceivedName, userEmail)));
         return this.list(queryWrapper);
        }
        return new ArrayList<>();
    }

    /**
     * 依照通知用户组id查询
     * @param notificationUserId
     * @return
     */
    @Override
    public List<CollectionNotificationReceived> queryByNotificationUserId(Long notificationUserId) {
        LambdaQueryWrapper<CollectionNotificationReceived> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CollectionNotificationReceived::getNotificationUserId,notificationUserId);
        return this.list(queryWrapper);
    }

    @Override
    public List<CollectionNotificationReceived> queryByNotificationUserIdS(List<Long> notificationUserId) {
        if(CollectionUtil.isNotEmpty(notificationUserId)){
            LambdaQueryWrapper<CollectionNotificationReceived> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.in(CollectionNotificationReceived::getNotificationUserId,notificationUserId);
            return this.list(queryWrapper);
        }
        return new ArrayList<>();
    }

    /**
     * 依照通知用户组id删除
     * @param notificationUserId
     */
    @Override
    public void delByNotificationUserId(Long notificationUserId) {
        LambdaUpdateWrapper<CollectionNotificationReceived> updatePmcSalesArea = Wrappers.lambdaUpdate();
        updatePmcSalesArea.eq(CollectionNotificationReceived::getNotificationUserId,notificationUserId);
        collectionNotificationReceivedMapper.delete(updatePmcSalesArea);
    }

    /**
     * 批量保存通知用户组管理
     * @param collectionNotificationReceived
     */
    @Transactional
    @Override
    public void bathSaveCollectionNotificationReceived(List<CollectionNotificationReceived> collectionNotificationReceived) {
        this.saveBatch(collectionNotificationReceived);
    }

    /**
     * 批量更新通知用户组管理
     * @param collectionNotificationReceived
     * @param id
     */
    @Transactional
    @Override
    public void bathUpdateCollectionNotificationReceived(List<CollectionNotificationReceived> collectionNotificationReceived, Long id) {
        this.delByNotificationUserId(id);
        this.saveOrUpdateBatch(collectionNotificationReceived);
    }
}
